//
//  AddSuccessViewController.m
//  RONGAO
//
//  Created by Kendama on 2018/2/26.
//  Copyright © 2018年 wei wang. All rights reserved.
//

#import "AddSuccessViewController.h"
#import "BorderTextFieldView.h"
#import "EditNumInfoAPi.h"
#import "MemberPwdModel.h"

@interface AddSuccessViewController ()<UITableViewDelegate,UITableViewDataSource>

/** UItableView **/
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UILabel *numberLabel;
@property(nonatomic,strong) BorderTextFieldView *nicknameTF;
@property(nonatomic,strong) UIButton *saveButton;

@end

@implementation AddSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configMemberInfo];
}

- (void)configMemberInfo {
    self.numberLabel.text = self.memberModel.user_no;
}

#pragma mark - --------------------------点击事件--------------------------
- (void)didClickSaveButton
{
    __weak typeof(self) weak = self;
    NSString *name = self.nicknameTF.textField.text;
    if (name.length != 0 && self.memberModel.id.length != 0) {
        [EditNumInfoAPi editNumInfo:self.memberModel.id name:name success:^(NSDictionary *dic) {
            NSLog(@"dic:%@",dic);
            if ([dic[@"errcode"] intValue] == 0) {
                [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weak.navigationController popViewControllerAnimated:YES];
                });
            }
        } fail:^(NSDictionary *dic) {
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }];
    }
}

#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    return self.dataSoure.count;
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

#pragma mark - --------------------------UITableView delegate--------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = [UIColor grayColor];
    __weak UIView *superView = cell.contentView;
    if (indexPath.row == 0)
    {
        NSString * holder = @"";
        switch (self.type) {
            case 1:
                holder = @"指纹编号";
                break;
            case 2:
                holder = @"密码编号";
                break;
            case 3:
                holder = @"卡片编号";
                break;
                
            default:
                break;
        }
        cell.textLabel.text = holder;
        [superView addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(superView.mas_left).with.offset(90);
            make.centerY.mas_equalTo(superView.mas_centerY);
        }];
    }
    else
    {
        cell.textLabel.text = @"昵称";
        [superView addSubview:self.nicknameTF];
        [self.nicknameTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(superView.mas_left).with.offset(90);
            make.centerY.mas_equalTo(superView.mas_centerY);
            make.height.mas_equalTo(30);
            make.right.mas_equalTo(superView.mas_right).with.offset(-30);
        }];
    }
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


#pragma mark - --------------------------懒加载--------------------------
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (UILabel *)numberLabel
{
    if(!_numberLabel)
    {
//        UIView *superView = self.view;
        _numberLabel = [[UILabel alloc]init];
//        [superView addSubview:_numberLabel];
        [_numberLabel setFont:[UIFont systemFontOfSize:14]];
        _numberLabel.text = @"09";
//        [_numberLabel setTextColor:<#(UIColor * _Nullable)#>];
    }
    return _numberLabel;
}
- (BorderTextFieldView *)nicknameTF
{
    if(!_nicknameTF)
    {
        UIView *superView = self.view;
        _nicknameTF = [[BorderTextFieldView alloc]init];
        [superView addSubview:_nicknameTF];
//        _nicknameTF.layer.cornerRadius = 5;
//        _nicknameTF.layer.borderColor = [UIColor lightGrayColor].CGColor;
//        _nicknameTF.layer.borderWidth = 1;
        _nicknameTF.textField.placeholder = @"请输入您的昵称";
    }
    return _nicknameTF;
}

- (UIButton *)saveButton
{
    if(!_saveButton)
    {
        UIView *superView = self.view;
        _saveButton = [[UIButton alloc]init];
        [superView addSubview:_saveButton];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_saveButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_saveButton setBackgroundImage:[UIImage imageNamed:@"button_next_nor"] forState:UIControlStateNormal];
        [_saveButton addTarget:self action:@selector(didClickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}


#pragma mark - --------------------------约束--------------------------
- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    superView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(116);
    }];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(200);
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.left.mas_equalTo(superView.mas_left).with.offset(pxScaleWidth(65));
        make.right.mas_equalTo(superView.mas_left).with.offset(pxScaleWidth(-65));
    }];
}

#pragma mark - --------------------------导航栏--------------------------
- (void)setNavigationStyle
{
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    /** type-1(指纹),2(密码),3(卡片) */
    NSString *nvTitle = @"";
    switch (self.type) {
        case 1:
            nvTitle = @"指纹详情";
            break;
        case 2:
            nvTitle = @"密码详情";
            break;
        case 3:
            nvTitle = @"卡片详情";
            break;
            
        default:
            break;
    }
    [self setNavigationTitle:nvTitle titleColor:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
